package com.bll.imple.bbsAction;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.BbsBean;
import com.bll.Action;
import com.dao.BbsDao;
import com.dao.Page;

public class ShowBbsAction implements Action{
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		HttpSession session=request.getSession();
		int userId=Integer.parseInt(session.getAttribute("userId").toString());
	    String lujing=request.getParameter("lujing");
	    BbsDao dao=new BbsDao();
	    List<BbsBean> beans =dao.selectAll(userId);
	    
	    int totalRows=beans.size();
	    int rowsPerPage=5;
	    int currentPage=1;
	    int totalPages=Page.computer(totalRows, rowsPerPage);
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
			if(currentPage<1){
			
				currentPage=1;
			}else if(currentPage>totalPages){
			
				currentPage=totalPages;
			}
		}
		int currentRows=(currentPage-1)*rowsPerPage;
//		if(request.getParameter("currentRows")!=null){
//			currentRows=Integer.parseInt(request.getParameter("currentRows"));
//		}
		//System.out.println("currenet:"+currentRows);
	    
		request.setAttribute("currentPage", currentPage);
	    request.setAttribute("totalPages", totalPages);
	    List<BbsBean> list =dao.selectPages(currentRows, rowsPerPage, userId);
	    //System.out.println("bean:"+bean);
	    request.setAttribute("bean", list);
		
//		HttpSession session=request.getSession();
//		int userId=Integer.parseInt(session.getAttribute("userId").toString());
//	    String lujing=request.getParameter("lujing");
//	    BbsDao dao=new BbsDao();
//	    List<BbsBean> list =dao.selectAll(userId);
//	    //System.out.println("bean:"+bean);
//	    request.setAttribute("bean", list);
		return lujing;
	}
}
